$(document).ready(function(){
	var ctx = $("#ctx").val();//获取服务器地址
	
	if($('#calendar_span').html()){
		$('#dateRangePicker').dateRangePicker();
		if($('#dateRangePicker').val() == ""){
			$('#dateRangePicker').val(getlastDate(1)+" to "+getlastDate(1));
			$("#date #yesterday").addClass('dateChange');
			$("#dateTab").val("yesterday");
		}
	}
	
	
	/***************************    订单列表页    ***************************/
	$("#btn-create").click(function(){
		if($("#advertiserId").val() == "" || $("#advertiserId").val() == -1){
			$(this).attr("href","javascript:void(0);");
			bootbox.alert("请选择广告主！");
			$(this).attr("title","请选择广告主");
			return false;
		}
	});

	$('ul#advertiser_son_ul li').live("click",function(){
		$("#myform").attr("action",ctx+"/advertiser/main/ad/order/list?advertiserId="+$("#advertiserId").val());
		refreshData();
	});
	
 	//状态全部、开启、停用
 	if($("#status").val() == 2 || $("#status").val() == ""){
 		$("#selectBar_status a:first").addClass("selectBar_change");
	}
 	$("#selectBar_status a").each(function(){
 		var value = $(this).attr("value");
 		if($("#status").val() == value){
 			$(this).addClass("selectBar_change").siblings('.selectBar_change').removeClass('selectBar_change');
 		}
 	});
 	$("#selectBar_status a").click(function(){
 		$("#status").val($(this).attr("value"));
 		refreshData();
 	});
 
	//请选择点击类型
 	if($("#main_perClickul").html()){
 		if($("#perClickType").val() == 0 || $("#perClickType").val() == ""){
 			$("#perClickul_span").text("请选择点击类型");
 		}
 		$('ul#perClick_son_ul li').each(function(){
 			if($(this).attr("value") == $("#perClickType").val()){
 				$("#perClickul_span").text($(this).text());
 			}
 		});
 		$("#perClick_son_ul").hide();
 		$('ul#perClick_son_ul li').live("click",function(){
 			$(this).parents('li').find('span').text($(this).html());
 			$("#perClickType").val($(this).attr("value"));
 			$("#perClickTypeName").val($(this).text());
 			$('#perClick_son_ul').hide(); //ul隐藏
 		});
 		$("#perClickul_span").on("click",function(){
 	 		$('#perClick_son_ul').show(); //ul显示
 	 	});
 		$("#perClickul_box").mouseleave(function(){
 	 		$('#perClick_son_ul').hide(); //ul隐藏
 	 	});
 	}
 	
 	//请选择曝光类型
 	if($("#main_perImpressionul").html()){
 		if($("#perImpressionType").val() == 0 || $("#perImpressionType").val() == ""){
 			$("#perImpressionul_span").text("请选择曝光类型");
 		}
 		$('ul#perImpression_son_ul li').each(function(){
 			if($(this).attr("value") == $("#perImpressionType").val()){
 				$("#perImpressionul_span").text($(this).text());
 			}
 		});
 		$("#perImpression_son_ul").hide();
 		$('ul#perImpression_son_ul li').live("click",function(){
 			$(this).parents('li').find('span').text($(this).html());
 			$("#perImpressionType").val($(this).attr("value"));
 			$("#perImpressionTypeName").val($(this).html());
 			$('#perImpression_son_ul').hide(); //ul隐藏
 		});
 		$("#perImpressionul_span").on("click",function(){
 	 		$('#perImpression_son_ul').show(); //ul显示
 	 	});
 		$("#perImpressionul_box").mouseleave(function(){
 	 		$('#perImpression_son_ul').hide(); //ul隐藏
 	 	});
 	}
 	
	//批量开启,批量暂停
	$("#all-btn-enable,#all-btn-disable,#all-btn-delete").click(function(){
		if($(this).attr("id") == "all-btn-enable"){
			url = ctx+"/advertiser/main/ad/order/enable";
		}else if($(this).attr("id") == "all-btn-disable"){
			url = ctx+"/advertiser/main/ad/order/disable";
		}else if($(this).attr("id") == "all-btn-delete"){
			url = ctx+"/advertiser/main/ad/order/delete";
		}
		batchOpear($(this),url);
	});
	
	/***************************    创建，编辑广告主    ***************************/
	
	//初始校验表单
 	if($("#validateForm").html()){
 		$("#validateForm").autovalidate();
 	}
 	
 	//点击日期控件的按钮时，去掉前面时间标签的样式
	$(".apply-btn").click(function(){
		if(!$(this).hasClass("disabled")){
			$("#dateTab").val("");
			$('#dateChoose_span a').removeClass('dateChange');
			$('#dateRangePicker').val($(".start-day").text() +" to "+$(".end-day").text());
		}
	});
	
	$(".btn-save").click(function(){
	    if($("#ownerId").val() == "" || $("#ownerId").val() == undefined ){
            alert("请选择订单所属人！");
            return false;
        }
		if (!$("#validateForm").validate()) {
			return false;
		}
		var falg = validateMoney();
		if(falg == false){
			return false;
		}
		var dateRange = $('#dateRangePicker').val();
		var dateArr = dateRange.split(" to ");
		$('#dateStart').val(dateArr[0]);
		$('#dateEnd').val(dateArr[1]);
		//暂时没有js插件，先使用split方法作为订单排期的结束和开始值
		$("#validateForm").submit();
		return false;
	});
	
	$(".btn-cancel").click(function(){
		window.location.href = ctx+"/advertiser/main/ad/order/list?advertiserId="+$("#advertiserId").val();
	});
	
	$("#budget").blur(function(){
		validateMoney();
	});
	
	/***************************    启用 暂停按钮    ***************************/
	$('.switch input').change(function(){
		if( $(this).is(':checked') ){
			url = ctx+"/advertiser/main/ad/order/enable";
			$(this).parent().siblings('.switchText').html('开启');
		}else{
			url = ctx+"/advertiser/main/ad/order/disable";
			$(this).parent().siblings('.switchText').html('暂停');
		}
		singleOper($(this),url);
	});
	
	//删除
	$(".btn-delete").click(function(){
		url = ctx+"/advertiser/main/ad/order/delete";
		singleOper($(this),url);
	});
	
});
		

function exportExcel_list(){
	var searchValue = $("#searchValue").val();
	if(searchValue == "请输入关键字"){
		searchValue = "";
	}
	var param = "?advertiserId="+$("#advertiserId").val()+"&status="+$("#status").val()+"&searchValue="+searchValue;
	$("#exportExcel").attr("href",$("#ctx").val()+"/advertiser/main/ad/order/exportDataReportExcel"+param);
	return false;
}
//删除
function del() {
	url = ctx+"/advertiser/main/ad/order/delete";
	singleOper($(this),url);
}
function validateMoney(){
	var reg=/^\d{1,8}\.?\d{0,2}$/;
	var $this = $("#budget");
	if($this.val().match(reg)!=null){
		if(($this.val().indexOf('.') == -1 && $this.val().length > 8) || $this.val() < 0){
			$this.css("box-shadow","0 0 6px #ff0000").attr('title','总预算金额整数位最多8位');
			$this.parent("div").find("span.spanVaildate").html("总预算金额整数位最多8位");
			return false;
		}else if($this.val() > parseInt($("#balance").val())){
			$this.css("box-shadow","0 0 6px #ff0000").attr('title','总预算不能大于该广告主的余额');
			$this.parent("div").find("span.spanVaildate").html("总预算不能大于该广告主的余额");
			return false;
		}else{
			$this.css("box-shadow","none").attr('title','');
			$this.parent("div").find("span.spanVaildate").html("");
		}
	}else{
		$this.css("box-shadow","0 0 6px #ff0000").attr('title','总预算金额整数位最多8位且最多有2位小数');
		$this.parent("div").find("span.spanVaildate").html("总预算金额整数位最多8位且最多有2位小数");
		return false;
	}
}
